package com.edu.hre.db.sql.pim;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreEmpLicense;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpLicenseForm;
import com.edu.hre.struts.form.pim.EmpLicenseListForm;

public class EmpLicenseDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "empLicenseDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<EmpLicenseForm> getEmpLicenseList(int limit, int offset, int empNumber){

		ArrayList<EmpLicenseForm> listResult = new ArrayList<EmpLicenseForm>();

		String sql = "SELECT elic.id AS col1," +
					" elic.license_no AS col2," +
					" lic.name AS col3," +
					" elic.license_issued_date AS col4, " +
					" elic.license_expiry_date AS col5 " +

					" FROM hre_emp_license AS elic" +
					" LEFT OUTER JOIN hre_license AS lic ON elic.license_id = lic.id" +
					" LEFT OUTER JOIN hre_employee AS emp ON elic.emp_number = emp.emp_number " +

					" WHERE emp.is_deleted = 0 " +
					" AND emp.emp_number = ? " +
					" ORDER BY elic.date_created DESC";
		
		List <Object[]> listObject = baseService.listArrayObject(sql, limit, offset, empNumber);
		for(int i = 0; listObject != null && i < listObject.size(); i++){

			try {

				Object[] objs = listObject.get(i);
				EmpLicenseForm license = new EmpLicenseForm();
	
				license.setId(Integer.parseInt(CommonUtils.normalizeObjectToString(objs[1], "0")));
				license.setEmpNumber(empNumber);
				license.setLicenseNo(CommonUtils.normalizeObjectToString(objs[2], ""));
				license.setLicenseType(CommonUtils.normalizeObjectToString(objs[3], ""));
				
				try {
					license.setIssuedDate(CommonUtils.dateToString((Date) objs[4]));
					license.setExpiryDate(CommonUtils.dateToString((Date) objs[5]));
				} catch (Exception e) {
					CommonUtils.println(e.getMessage());
				}

				listResult.add(license);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return listResult;
	}

	public int countEmpLicenses(int empNumber){

		int count = baseService.countByColumns(new String[]{"empNumber"}, new int[]{Compare.EQUAL}, new Integer[]{empNumber}, "id", HreEmpLicense.class);
		return count < 0 ? 0 : count;
	}

	public void deleteLicenses(EmpLicenseListForm form){

		ArrayList<EmpLicenseForm> info = (ArrayList<EmpLicenseForm>) form.getAllLicenses();

		for (int i = 0; i < info.size(); i++) {
			
			try {

				EmpLicenseForm work = info.get(i);
				if (work.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, (long) work.getId(), null, null, 0, HreEmpLicense.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
